package pl.freetimetool.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pl.freetimetool.dao.ProjectDao;
import pl.freetimetool.domain.Project;

@Transactional
@Repository
public class ProjectServiceImpl implements ProjectService {

	@Autowired
	private ProjectDao projectDao;

	@Transactional(readOnly = true)
	public void addNewProject(final Project project) {
		if (this.projectDao.findProject(project.getProjectName()) == null) {
			this.projectDao.saveProject(project);
		}
	}

	@Transactional(readOnly = true)
	public boolean removeProject(final String projectName) {

		final Project project = this.projectDao.findProject(projectName);

		if (project == null) {
			return false;
		} else {
			this.projectDao.deleteProjectFromDatabase(project);
			return true;
		}
	}

	@Transactional(readOnly = true)
	public Project getProject(final String projectName) {
		return this.projectDao.findProject(projectName);
	}

	@Transactional(readOnly = true)
	public List<Project> findAllProjects() {
		return this.projectDao.findAllProjects();
	}
}
